/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-12-21     lenovo       the first version
 */
#include "led_app.h"

rt_mutex_t ucled_mutex;

#define LED_PIN_BASE GET_PIN(C,8)
#define LED_PIN_LOAD GET_PIN(D,2)

uint8_t ucled[8]={0,1,0,1,0,1,0,1};

void led_disp(uint8_t *ucled)
{
    uint8_t temp=0x00;
    static uint8_t temp_old=0xff;
    for(uint8_t i=0;i<8;i++)
    {
        temp|=(ucled[i]<<i);
    }
    if(temp!=temp_old)
    {
      for(uint8_t i=0;i<8;i++)
      {
           if((temp>>i)&0x01)
           {
             rt_pin_write(LED_PIN_BASE+i, PIN_LOW);
           }
           else
           {
             rt_pin_write(LED_PIN_BASE+i, PIN_HIGH);
           }
      }
      rt_pin_write(LED_PIN_LOAD, PIN_HIGH);
      rt_pin_write(LED_PIN_LOAD, PIN_LOW);
    }
    temp_old=temp;
}

void led_proc(void *parameter)
{
    while(1)
    {
        rt_mutex_take(ucled_mutex, RT_WAITING_FOREVER);
        led_disp(ucled);
        rt_mutex_release(ucled_mutex);
        rt_thread_mdelay(100);

    }
}

void led_init(void)
{
    for(uint8_t i=0;i<8;i++)
    {
        rt_pin_mode(LED_PIN_BASE+i, PIN_MODE_OUTPUT);
    }

    ucled_mutex = rt_mutex_create("ucled_mutex", RT_IPC_FLAG_FIFO);
    if (ucled_mutex == RT_NULL)
    {
        rt_kprintf("Failed to create ucled_mutex\n");
        return;
    }
    rt_pin_mode(LED_PIN_LOAD, PIN_MODE_OUTPUT);
    rt_thread_t led_thread = rt_thread_create("led_proc",led_proc,RT_NULL,1024,20,10);
    if(led_thread!=RT_NULL)
       rt_thread_startup(led_thread);

}
